<?php


namespace App\Admin\Services\App;


use App\Admin\Exceptions\App\ProjectType\ProjectTypeException;
use App\Admin\Requests\App\ProjectType\ProjectTypeCreateRequest;
use App\Admin\Requests\App\ProjectType\ProjectTypeUpdateAuditItemListRequest;
use App\Admin\Requests\App\ProjectType\ProjectTypeUpdateDomainRequest;
use App\Admin\Requests\App\ProjectType\ProjectTypeUpdateRequest;
use App\Commons\WechatOpenPlatform\WechatOpenPlatformTool;
use App\Repositories\App\AppProjectRepository;
use App\Repositories\App\AppProjectTypeRepository;
use App\Repositories\Wechat\MiniProgramTemplateRepository;

class ProjectTypeService {
    private $_repository;

    public function __construct(AppProjectTypeRepository $repository) {
        $this->_repository = $repository;
    }

    /**
     * 应用项目类型列表
     *
     * @return mixed
     */
    public function page() {
        return $this->_repository->queryByConditionAndPage();
    }

    /**
     * 新建应用项目类型
     *
     * @param ProjectTypeCreateRequest $request
     * @return mixed
     */
    public function store(ProjectTypeCreateRequest $request) {
        $fillData = $request->post();
        $fillAble = $request->getRuleKeys();
        //获取配置域名和审核列表
        $fillData['server_domain']             = WechatOpenPlatformTool::getOpenPlatformConfigWechatMiniProgramCommonServerDomain();
        $fillData['webviewdomain']             = WechatOpenPlatformTool::getOpenPlatformConfigWechatMiniProgramCommonSetWebViewDomain();
        $fillData['wx_submit_audit_item_list'] = [];
        $fillAble                              = array_merge($fillAble, ['server_domain', 'webviewdomain', 'wx_submit_audit_item_list']);
        $appProjectTypeEl                      = $this->_repository->getEloquent()->fillable($fillAble)->fill($fillData);
        $appProjectTypeEl->save();
        return $appProjectTypeEl;
    }

    /**
     * 删除指定id的应用项目类型
     *
     * @param $id
     * @return \Illuminate\Database\Eloquent\Model|mixed
     * @throws ProjectTypeException
     */
    public function delete($id) {
        $appProjectEl = (new AppProjectRepository())->getItemByConditions(['app_project_type_id' => $id]);
        if ($appProjectEl->isNotEmpty()) {
            throw new ProjectTypeException(1100002);
        }
        $miniProgramTemplateEl = (new MiniProgramTemplateRepository())->getItemByConditions(['app_project_type_id' => $id]);
        if ($miniProgramTemplateEl->isNotEmpty()) {
            throw new ProjectTypeException(1100003);
        }
        return $this->_repository->delete($id, null, new ProjectTypeException(110001));
    }

    /**
     * 更新指定id的应用项目类型
     *
     * @param $id
     * @param ProjectTypeUpdateRequest|ProjectTypeUpdateDomainRequest|ProjectTypeUpdateAuditItemListRequest $request
     * @return \Illuminate\Database\Eloquent\Model|mixed
     * @throws \Exception
     */
    public function update($id, $request) {
        return $this->_repository->update($id, $request->post(), $request->getRuleKeys(), new ProjectTypeException(110001));
    }

    /**
     * 获取可用的应用项目类型下拉列表
     *
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function listForSelect() {
        return $this->_repository->getItemByConditions(
            [],
            function ($query) {
                $query->select(['id', 'name', 'server_domain', 'webviewdomain']);
            }
        );
    }

    /**
     * 修改应用项目类型服务器域名及业务域名
     *
     * @param $id
     * @param ProjectTypeUpdateDomainRequest $request
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|object|null
     * @throws ProjectTypeException
     */
    public function updateDomain($id, ProjectTypeUpdateDomainRequest $request) {
        $appProjectTypeEl = $this->_repository->getItemByConditions(['id' => $id], null, true);
        if ($appProjectTypeEl) {
            $fillData = $request->post();
            $fillAble = $request->getRuleKeys();
            $appProjectTypeEl->fillable($fillAble)->fill($fillData);
            $appProjectTypeEl->save();
        } else {
            throw new ProjectTypeException(110001);
        }
        return $appProjectTypeEl;
    }
}